package com.example.controller;


import com.example.pojo.User;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("users")
public class userController {

    @Autowired
    private UserService userService;

    @RequestMapping("/findAll")
    public String findAll(Model model){
        List<User> list = userService.findAll();
        model.addAttribute("list",list);
        return "list";
    }
    @PostMapping("/addUser")
    public String addUser(User users){
        userService.addUser(users);
        return "redirect:findAll";
    }
    @RequestMapping("/add")
    public String add(){
        return "add";
    }
    @RequestMapping("/delete/{id}")
    public String delete(@PathVariable("id") Integer id){
        userService.delete(id);
        return "redirect:/users/findAll";
    }
    @RequestMapping("/findById/{id}")
    public String findById(@PathVariable("id")Integer id,Model model){
        User user = userService.findById(id);

        model.addAttribute("user",user);
        return "update";
    }
    @PostMapping("/update/{id}")
    public String user(User user,@PathVariable("id")Integer id){
        System.out.println(user);
        userService.update(user,id);
        return "redirect:/users/findAll";
    }

}
